package com.medic.service.impl;


import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.medic.entity.Doctor;
import com.medic.entity.Weichat;
import com.medic.mapper.interf.DoctorMapper;
import com.medic.service.interf.DoctorServiceInterface;
import com.medic.util.CacheUtil;


@Service("doctorService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)// 注明需要回滚
public class DoctorServiceImpl implements DoctorServiceInterface {

	
	@Resource
	private DoctorMapper doctorMapper;
	

	
	
	@Resource
	private WeichatServiceImpl weichatService;

	@Override
	public boolean delete(Doctor object) throws Exception {
		doctorMapper.delete(object);
		return true;
	}
	@Override
	public boolean update(Doctor object) throws Exception {
		doctorMapper.update(object);
		return true;
	}

	@Override
	public boolean save(Doctor dcr) throws Exception {
		doctorMapper.save(dcr);
		
		Weichat wechat = new Weichat();
		wechat.setOpenid(dcr.getOpenid());
		wechat.setRole("doctor");
		wechat.setRolename(dcr.getName());
		wechat.setRoleid(dcr.getId());
		
		weichatService.save(wechat);
		
		return true; 
	}
	
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	@Override
	public Page<Doctor> getDoctor(int pageno, String where) throws Exception {
		PageHelper.startPage(pageno,  CacheUtil.getRole_pagesize());
		
		Page<Doctor> page = (Page<Doctor>)doctorMapper.selectDoctor(where);
		
		return page;
	}
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Doctor getDoctor(int doctorid) throws Exception {
		
		return doctorMapper.selectDoctorByID(doctorid );
	}	


}